/**
 * created by 江心才子, 2019/8/6
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.mine.collection

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wyl.base.ACacheHelper
import com.wyl.base.MineCollectDelete
import com.wyl.base.MineCollectList
import com.wyl.base.MineCollectMoreList
import com.wyl.base.bean.ListData
import com.wyl.base.bean.VideoData
import com.wyl.libbase.base.PageViewModel
import com.wyl.libbase.utils.KLog
import com.wyl.libbase.extend.logD
import io.reactivex.Observable

/**
 * 项目名称：ShortVideo
 * 创建人：江心才子
 * 创建时间：2019-08-06 10:09
 * 内容描述：
 * 修改说明：
 */

class CollectionViewModel : PageViewModel() {

    val dataSource = ObservableArrayList<VideoData>()

    init {
        enableRefresh.set(false)
    }

    override fun refresh() {
        OkGo.post<BaseResponse<ListData>>(MineCollectList)
            .params("appUserId", ACacheHelper.id)
            .converter(object : JsonConvert<BaseResponse<ListData>>() {})
            .adapt(ObservableBody<BaseResponse<ListData>>())
            .doFinally { refresh.set(false) }
            .map { it.data }
            .flatMap { data ->
                data.record1?.apply {
                    forEach { it.group = "今天" }
                    dataSource.addAll(this)
                }
                data.record2?.apply {
                    forEach { it.group = "七天内" }
                    dataSource.addAll(this)
                }
                loadMoreData()
            }
            .doOnNext {
                enableLoadMore.set(++page < it.totalPage)
                KLog.d("loadmore :${enableLoadMore.get()}  page = $page  totalPage = ${it.totalPage}")
            }
            .map { it.data }
            .subscribe(
                { data ->
                    data.apply {
                        forEach { it.group = "更早" }
                        dataSource.addAll(this)
                    }
                }, {
                    it.printStackTrace()
                }
            ).toDisposables(disposables)
    }

    private fun loadMoreData(): Observable<BaseResponse<List<VideoData>>> =
        OkGo.post<BaseResponse<List<VideoData>>>(MineCollectMoreList)
            .params("appUserId", ACacheHelper.id)
            .params("pageSize", pageSize)
            .params("pageNum", page)
            .converter(object : JsonConvert<BaseResponse<List<VideoData>>>() {})
            .adapt(ObservableBody<BaseResponse<List<VideoData>>>())

    override fun loadMore() {
        loadMoreData()
            .doFinally { loadMore.set(false) }
            .doOnNext {
                enableLoadMore.set(++page < it.totalPage)
                KLog.d("loadmore :${enableLoadMore.get()}  page = $page  totalPage = ${it.totalPage}")
            }
            .map { it.data }
            .subscribe { data ->
                data.apply {
                    forEach { it.group = "更早" }
                    dataSource.addAll(this)
                }
            }.toDisposables(disposables)
    }


//    编辑操作

    var isOperation = false

    var operation = ObservableField("编辑")
    var operationVisibable = ObservableInt(View.GONE)

    /**
     * 点击右侧编辑按钮
     */
    fun operation() {
        isOperation = if (!isOperation) {
            operation.set("取消")
            operationVisibable.set(View.VISIBLE)
            true
        } else {
            operation.set("编辑")
            operationVisibable.set(View.GONE)
            dataSource.forEach { it.selected = false }
            false
        }
    }

    fun selectAll() {
        if (dataSource.any { !it.selected }) {
            dataSource.forEach { it.selected = true }
        } else {
            dataSource.forEach { it.selected = false }
        }
    }

    fun delete() {
        val id = dataSource.filter { it.selected }
            .joinToString(",") { it.id }
        logD("id = $id")
        OkGo.post<BaseResponse<List<VideoData>>>(MineCollectDelete)
            .params("appUserId", ACacheHelper.id)
            .params("id", id)
            .converter(object : JsonConvert<BaseResponse<List<VideoData>>>() {})
            .adapt(ObservableBody<BaseResponse<List<VideoData>>>())
            .doOnSubscribe { loading.value = true }
            .doFinally { loading.value = false }
            .subscribe(
                {
                    dataSource.removeAll { it.selected }
                }, {
                    it.printStackTrace()
                }
            ).toDisposables(disposables)
    }
}